package shops;

import java.util.*;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;

public class main {
    
    private int pid;
     private  ProductItem pt;
    
    
    public main() {
    }
    
    public String execute() throws Exception {
       
       // HttpServletRequest request=ServletActionContext.getRequest();   
        //pid=3;
           
        NWDB.NWDB mydb=new NWDB.NWDB();
        NWDB.IProduct ip=new NWDB.ImplProduct(mydb.getConnection());
        mydb.setIProduct(ip);
        
        //網址列pid不可超過商品總數
        if(getPid()>mydb.GetAllProductCount()||getPid()<=0)
            setPid(1);
        
        HashMap<String,Object> product=mydb.GetProductItemById(getPid());
        pt = new ProductItem();
        pt.setId(getPid());
        pt.setName(product.get("Name").toString());
        pt.setDescription(product.get("Description").toString());
        pt.setPrice((Double)product.get("Price"));
        pt.setType(product.get("TypeName").toString());
        pt.setStock((Short)product.get("Stock"));
      
        /*  
        if(p==0) p=1;
        
        NWDB.NWDB mydb=new NWDB.NWDB();
        NWDB.IProduct ip=new NWDB.ImplProduct(mydb.getConnection());
        mydb.setIProduct(ip);

        result=mydb.GetProductByPage(p);
        
        //取得產品頁數
        int totalPage=mydb.GetProductCount();
        totalPage=(int)(Math.ceil(totalPage/10.0));

        StringBuilder sb = new StringBuilder();
        for (int i = 1; i <= totalPage; i++)
        {
            if (i == p)
                sb.append(i).append("&nbsp;&nbsp;");
            else
                sb.append(String.format("<a href='/MyStruts2/shop/?p=%d'>%d</a>&nbsp;&nbsp;", i, i));
        }
        this.pageList=sb.toString();
        
        mydb.close();*/
        
        
        
        return "success";
    }

 

    /**
     * @return the pt
     */
    public ProductItem getPt() {
        return pt;
    }

    /**
     * @param pt the pt to set
     */
    public void setPt(ProductItem pt) {
        this.pt = pt;
    }

    /**
     * @return the pid
     */
    public int getPid() {
        return pid;
    }

    /**
     * @param pid the pid to set
     */
    public void setPid(int pid) {
        this.pid = pid;
    }

  

}